{
 "cells": [
  {
   "metadata": {},
   "cell_type": "raw",
   "source": [
    "某商店每小时销售额Series:\n",
    "### 1.按天重采样计算每日总销售额\n",
    "### 2.计算每天营业时间(8:00-22:00)和非营业时间的销售额比例\n",
    "### 3.找出销售额最高的3个小时"
   ],
   "id": "395b378514381934"
  },
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-11-24T03:22:28.646292Z",
     "start_time": "2025-11-24T03:22:28.631104Z"
    }
   },
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ],
   "outputs": [],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-24T05:15:18.027631Z",
     "start_time": "2025-11-24T05:15:17.975468Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 数据准备\n",
    "np.random.seed(42)\n",
    "hourly_sales = pd.Series(np.random.randint(0, 100, 24),\n",
    "                         index=pd.date_range('2025-01-01', periods=24, freq='h'))\n",
    "print(hourly_sales)"
   ],
   "id": "aa82811e456ec81f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2025-01-01 00:00:00    51\n",
      "2025-01-01 01:00:00    92\n",
      "2025-01-01 02:00:00    14\n",
      "2025-01-01 03:00:00    71\n",
      "2025-01-01 04:00:00    60\n",
      "2025-01-01 05:00:00    20\n",
      "2025-01-01 06:00:00    82\n",
      "2025-01-01 07:00:00    86\n",
      "2025-01-01 08:00:00    74\n",
      "2025-01-01 09:00:00    74\n",
      "2025-01-01 10:00:00    87\n",
      "2025-01-01 11:00:00    99\n",
      "2025-01-01 12:00:00    23\n",
      "2025-01-01 13:00:00     2\n",
      "2025-01-01 14:00:00    21\n",
      "2025-01-01 15:00:00    52\n",
      "2025-01-01 16:00:00     1\n",
      "2025-01-01 17:00:00    87\n",
      "2025-01-01 18:00:00    29\n",
      "2025-01-01 19:00:00    37\n",
      "2025-01-01 20:00:00     1\n",
      "2025-01-01 21:00:00    63\n",
      "2025-01-01 22:00:00    59\n",
      "2025-01-01 23:00:00    20\n",
      "Freq: h, dtype: int32\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-24T03:27:34.055118Z",
     "start_time": "2025-11-24T03:27:34.014585Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 1.按天重采样计算每日总销售额\n",
    "total_sales = hourly_sales.resample('D')\n",
    "total_sales.sum()"
   ],
   "id": "49a61a2ee878a5b8",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2025-01-01    1205\n",
       "Freq: D, dtype: int32"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-24T12:01:01.715949Z",
     "start_time": "2025-11-24T12:01:01.698067Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 2.计算每天营业时间(8:00-22:00)和非营业时间的销售额比例\n",
    "print(\"方法一：between time\")\n",
    "# 总的销售额——>total_sales（记得求和）\n",
    "# 营业时间的销售额（记得求和）\n",
    "# 非营业时间的销售额 = 总的 - 营业时间的\n",
    "total_sales = hourly_sales.sum()\n",
    "business_total = hourly_sales.between_time('8:00', '22:00').sum()\n",
    "non_business_total = total_sales - business_total\n",
    "print(\"营业时间和非营业时间销售额比例：\", business_total / non_business_total)"
   ],
   "id": "bb3bc45b40608c63",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "方法一：between time\n",
      "营业时间和非营业时间销售额比例： 1.4294354838709677\n"
     ]
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-24T12:06:27.125605Z",
     "start_time": "2025-11-24T12:06:27.118862Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(\"方法二：布尔索引\")\n",
    "mask = (hourly_sales.index.hour >= 8) & (hourly_sales.index.hour <= 22)\n",
    "business_series = hourly_sales[mask]\n",
    "non_business_series = hourly_sales[~mask]\n",
    "# !!! 注意：上方获取的只是列表，求和才是营业时间和非营业时间分别的销售额总和\n",
    "print(\"营业时间和非营业时间销售额比例：\", business_series.sum() / non_business_series.sum())"
   ],
   "id": "4b6ff6dad7464c6d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "方法二：布尔索引\n",
      "营业时间和非营业时间销售额比例： 1.4294354838709677\n"
     ]
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-24T12:12:17.688160Z",
     "start_time": "2025-11-24T12:12:17.636138Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 3.找出销售额最高的3个小时\n",
    "print(hourly_sales.nlargest(3).keys())"
   ],
   "id": "d4fb6daa901b7a6c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DatetimeIndex(['2025-01-01 11:00:00', '2025-01-01 01:00:00',\n",
      "               '2025-01-01 10:00:00'],\n",
      "              dtype='datetime64[ns]', freq=None)\n"
     ]
    }
   ],
   "execution_count": 20
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
